/**
 * 
 */
package soa.security.web.support.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;

import soa.security.domain.Department;
import soa.security.domain.Organization;
import soa.security.domain.Region;
import soa.security.mgr.service.AccountMgrService;
import soa.security.mgr.service.ApplsystemMgrService;
import soa.security.mgr.service.DepartmentMgrService;
import soa.security.mgr.service.GroupMgrService;
import soa.security.mgr.service.OrganizationMgrService;
import soa.security.mgr.service.PersonMgrService;
import soa.security.mgr.service.PositionMgrService;
import soa.security.mgr.service.RegionMgrService;
import soa.security.mgr.service.RoleMgrService;
import soa.security.mgr.service.TreeMgrService;
import soa.security.web.export.LdapJsonFormatExport;
import soa.security.web.export.LdapPageJsonFormatExport;
import v1.web.ExportableObject;
import v1.web.PageResult.MediaType;
import v1.web.export.DefaultPageJsonFormatExport;

/**
 * @author Cre.Gu
 *
 */
public abstract class LdapModelAction<T> extends DefaultModelAction<T> {
	private static final long serialVersionUID = 2369162184349096280L;

	@Resource
	protected RegionMgrService regionMgrService;
	@Resource
	protected OrganizationMgrService organizationMgrService;
	@Resource
	protected DepartmentMgrService departmentMgrService;
	@Resource
	protected PersonMgrService personMgrService;
	@Resource
	protected PositionMgrService positionMgrService;
	@Resource
	protected GroupMgrService groupMgrService;
	@Resource
	protected RoleMgrService roleMgrService;
	@Resource
	protected AccountMgrService accountMgrService;
	@Resource
	protected TreeMgrService treeMgrService;
	@Resource
	protected ApplsystemMgrService applsystemMgrService;

	protected String dnp;
	protected String parentDn;

	protected String organizationId;
	protected String orId;
	protected String departmentId;
	protected String accountDns;
	protected String departmentDns;
	protected String personDns;
	protected String personIds;

	protected boolean subTree;

	/**
	 * 排过序的dn
	 */
	protected String dns;
	
	

	@Override
	protected void updateResultContent(Object content) {
		if (content instanceof Page) { // 兼容以前版本，应该在默认的list方法中，直接创建DefaultPageJsonFormatExport
			content = new LdapPageJsonFormatExport(content);
		}

		super.updateResultContent(content);
	}

	@Override
	protected ExportableObject<?, ?> wrapContent(Object object, MediaType type) {
		if (object == null)
			return null;

		if (object instanceof ExportableObject<?, ?>)
			return (ExportableObject<?, ?>) object;

		return new LdapJsonFormatExport(object);
	}

	@Override
	public T getModel() {
		if (this.entity != null)
			return this.entity;

		if (entityClass == null) {
			log.warn("在{}类中无法反射模型类，所以无法创建模型对象", this.getClass().getName());
			return null;
		}

		T entity = null;
		try {
			String dn = getDnp();
			if (StringUtils.isBlank(dn) || StringUtils.contains(dn, ";")) {
				entity = (T) entityClass.newInstance();
			} else {
				entity = findByDn(dn);
			}
		} catch (Exception e) {
			log.warn("无法创建模型实例");
			return null;
		}

		return entity;
	}

	protected abstract T findByDn(String dn);

	public String remove() {
		baseMgrService.remove(dnp);
		return SUCCESS;
	}

	public String sort() {
		baseMgrService.sort(dns);
		return SUCCESS;
	}

	public String getDnp() {
		return dnp;
	}

	public void setDnp(String dnp) {
		this.dnp = dnp;
	}

	public String getParentDn() {
		return parentDn;
	}

	public void setParentDn(String parentDn) {
		this.parentDn = parentDn;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public String getDns() {
		return dns;
	}

	public void setDns(String dns) {
		this.dns = dns;
	}

	public String getPersonDns() {
		return personDns;
	}

	public void setPersonDns(String personDns) {
		this.personDns = personDns;
	}

	public String getPersonIds() {
		return personIds;
	}

	public void setPersonIds(String personIds) {
		this.personIds = personIds;
	}

	public String getDepartmentDns() {
		return departmentDns;
	}

	public void setDepartmentDns(String departmentDns) {
		this.departmentDns = departmentDns;
	}

	public boolean isSubTree() {
		return subTree;
	}

	public void setSubTree(boolean subTree) {
		this.subTree = subTree;
	}

	public String getOrId() {
		return orId;
	}

	public void setOrId(String orId) {
		this.orId = orId;
	}	

	public String getAccountDns() {
		return accountDns;
	}

	public void setAccountDns(String accountDns) {
		this.accountDns = accountDns;
	}

	public Organization getOrganization(String organizationId) {
		Organization result = null;

		if (StringUtils.isNotBlank(organizationId)) {
			List<Organization> list = organizationMgrService
					.getOrganization(organizationId);
			if (list.size() > 0) {
				result = list.get(0);
			}
		}

		return result == null ? new Organization() : result;
	}

	public Department getDepartment(String organizationId, String departmentId) {
		Department result = null;

		if (StringUtils.isNotBlank(organizationId)
				&& StringUtils.isNotBlank(departmentId)) {
			List<Department> list = departmentMgrService.getDepartment(
					organizationId, departmentId);
			if (list.size() > 0) {
				result = list.get(0);
			}
		}

		return result == null ? new Department() : result;
	}

	public Region getRegion(String regionDn) {
		Region result = null;

		if (StringUtils.isNotBlank(regionDn)) {
			result = regionMgrService.findByDn(regionDn);
		}

		return result == null ? new Region() : result;
	}
}
